/**
 * 地区表
 * @author platform
 * @email 15909910367@163.com
 * @date 2022-11-09 20:51:06
 */
Ext.define('Jason.view.protal.module.systemManager.sources.area.MainCtrl',{
	extend : 'Wys.basic.BaseCtrl',
	alias : 'controller.sysAreaViewCtrl',
	init:function(){
		var me = this;
		me.getView().getStore().load();
	},
	formItems:function(parentCode){
		return [{
				xtype : 'fieldset',
				title : '基本信息',
				collapsible: false,
				defaultType: 'textfield',
				layout : {
					type : 'table',
					columns : 2
				},
				defaults : {
					width : '100%',
					labelWidth : 80
				},
				items : [
					{fieldLabel:'id',name:'id',xtype:'textfield',value:'',hidden:true},
					{fieldLabel:'名称',name:'label',xtype:'textfield',allowBlank : false},
					{fieldLabel:'区划代码',name:'code',xtype:'textfield',allowBlank : false},
					{fieldLabel:'全称',name:'fullName',xtype:'textfield',allowBlank : false},
					{fieldLabel:'排序',name:'sortValue',xtype:'numberfield',minValue:1,maxValue:99,value:1,allowBlank : false},
					{fieldLabel:'经度',name:'longitude',xtype:'textfield'},
					{fieldLabel:'维度',name:'latitude',xtype:'textfield'},
					{fieldLabel:'区域等级',name:'level',
						xtype: 'RemoteCombobox',
	                    emptyText: '请选择...',
	                    model: Ext.create('Ext.data.Model', {
	                        fields: ['key', 'value']
	                    }),
	                    valueField: 'value',
	                    displayField: 'key',
	                    proxy_url: reqUrl.sys.comboData.dict + "AREALEVEL",
	                    allowBlank: false,
	                    afterLabelTextTpl: cfg.required,
	                    blankText: '区域等级不能为空',
	                    value: 1
					},
					{fieldLabel:'来源',name:'source',xtype:'textfield',allowBlank : false,value:'系统录入'},
					{fieldLabel:'parentId',name:'parentId',xtype:'textfield',value:(parentCode||null),hidden:true}
				]
		}];
	},
	onNewButtonClick : function(btn){
		var me = this;
		Ext.create('Wys.form.FormWindow',{
			width: 800,
			title : '行政区划',
			config : {
				operatetype : 'new',
				model : 'Jason.model.protal.module.systemManager.souces.area.AreaModel',
				gridPanel : btn.up('#sysAreaView'),
				items: me.formItems()
			}
		}).show();
	},
	onEditButtonClick : function(grid,record){
		var me = this;
		grid.setSelection(record);
		Ext.create('Wys.form.FormWindow',{
			width: 800,
			title : ' 『'+record.get('label') +'』行政区划' ,
			config : {
				model : 'Jason.model.protal.module.systemManager.souces.area.AreaModel',
				operatetype : 'edit',
				gridPanel : grid,
				items: me.formItems(),
				formData : record
			}
		}).show();
	},
	onDeleteButtonClick : function(btn){
		var grid = btn.up('#sysAreaView'),
			gridStore = grid.getStore(),
			selectGridData = grid.getSelection(),
			removePK = [],
			removeName = [];
		if(selectGridData.length===0){
			EUtil.toastWarn('请选择要操作的数据');
			return;
		}
		Ext.Array.each(selectGridData,function(item){
			removeName.push(item.data.title);
			removePK.push(item.data.id);
		});
		EUtil.showMsg({
			option:3,
			title : '温馨提示',
			message:'您确定删除<span style="color:red;">『'+removeName.join()+'』</span>吗？',
			callback:function(btn){
				if(btn === 'ok'){
					gridStore.remove(selectGridData);
					gridStore.sync({callback:function(){
						gridStore.reload();
	        	    }});
				}
			}
		});
	},
	onGridCommand: function (item, command, record, recordIndex, cellIndex) {
        var me = this;
        if (command === 'modify') {/** 修改 */
            me.onEditButtonClick(item.gridRef, record);
        } else if (command === 'add') {
            Ext.create('Wys.form.FormWindow', {
                width: 500,
                title: '组织机构',
                config: {
                    operatetype: 'new',
                    model: 'Jason.model.protal.module.systemManager.souces.area.AreaModel',
                    gridPanel: me.getView().down('treepanel'),
                    items: me.formItems(record.get('id'))
                }
            }).show();
        } else if (command === 'remove') {
            EUtil.showMsg({
                option: 3,
                title: '温馨提示',
                message: '您确定删除<span style="color:red;">『' + record.get('label') + '』</span>吗,？',
                callback: function (btn) {
                    if (btn === 'ok') {
                        EUtil.RS({
                            url: item.gridRef.getStore().getProxy().api.destroy,
                            params: { ids: record.get('id') },
                            callback: function (resp) {
                                if (resp.success) {
                                    EUtil.showSuccess(Ext.String.format(I18N.DeleteMessage[0],
                                        record.get('label')
                                    ));
                                    item.gridRef.getStore().reload();
                                } else {
                                    EUtil.showError(
                                        Ext.String.format(I18N.FailedOtherCode,
                                            resp.code,
                                            resp.msg)
                                    );
                                }
                            }
                        })
                    }
                }
            });
        }
    }
});